# 1. 引入pygame
import pygame as pg
from random import randint

from pygame.time import Clock

# 2. 初始化
pg.init()
# 2.1 窗口
screen = pg.display.set_mode((800,600)) # 窗口 高度和宽度
pg.display.set_caption("flappy bird")  # 窗口标题

#### 图片字体 #####
def text_image(name,size,text,color):
   # 创建新的系统字体对象
   font =pg.font.SysFont(name,size)
   # 文本渲染
   image = font.render(text,True,color)
   return image


# 5. 变量初始值
score = 0 # 分数
color = (100, 200, 0)
index = 0  # 小鸟列表下标
y = 200 # 小鸟位置
# color = "green" # 管道颜色
clock = pg.time.Clock() # 设置帧率
rects1 = []  # 上管道的集合
rects2 = []  #  下管道的集合
def layout():
    width = 60
    top = 0 # 管道上边的距离
    left = 200 # 管道离左边的距离
    x_distance = 300
    y_distance = 150  # 管道缺口的高度
    for i in range(10):
        height = randint(200, 400)  # 管道的高度
        rects1.append([left, top, width, height])  # 上管道
        rects2.append([left, height + y_distance, width, 600 - height - y_distance])  # 下管道
        left = left + x_distance
        ######## 高度和宽度 ########
        x_distance = x_distance - 10
        y_distance = y_distance - 5
layout()


flag  = False  #
########## 增加游戏难度 ###########


# 3. 图片路径
path = "source/"
bg  = pg.image.load(path + 'bg_day.png')
# 3.1 对图片进行处理
bg1 = pg.transform.smoothscale(bg,(800,600))

####### 设置帧率 #######
clock = pg.time.Clock()
# 4. 小鸟
birds = [
    pg.image.load(path+"bird1_0.png"),
    pg.image.load(path+"bird1_1.png"),
    pg.image.load(path+"bird1_2.png")
]

#### 加载结束图片
over = pg.image.load(path+'text_game_over.png')

# 2.2 事件监听
while True:

    ##### 帧率 #####
    clock.tick(30)
    # 3.1 背景图片
    screen.blit(bg1,(0,0))
    # 4.1 小鸟图片
    screen.blit(birds[index],(0,y))
    # 4.2 小鸟下落
    y  = y + 2
    if index == 2:
        index = 0
    else:
        index += 1

    for event in pg.event.get():
        if event.type == pg.QUIT:
            exit()
    # 6. 阻止小鸟下落
        if event.type == pg.KEYDOWN:
            if event.key == pg.K_SPACE:
                y = y - 30
            elif event.key == pg.K_r:
                if flag:
                    flag = False
                    layout()
                    y = 300
                    score = 0
    # 6.1 帧率
    clock.tick(30)
    # 管道移动的代码
    for i in range(len(rects1)):
        pg.draw.rect(screen, color, rects1[i])
        pg.draw.rect(screen, color, rects2[i])
        rects1[i][0] = rects1[i][0] - 4
        rects2[i][0] = rects2[i][0] - 4

    ##### 游戏得分 #####
    if rects1 and rects1[0][0] <= -60:
        rects1.pop(0)
        rects2.pop(0)
        score = score + 1

    ##### 绘制分数 #####
    img = text_image(None, 50, str(score), (255, 255, 255))
    screen.blit(img, (20, 20))

    ##### 碰撞检测 #####
    # 上管道
    # left < 48 and height > y
    # 下管道检测 
    # left < 48 and top < y + 48
    if rects1:
        if(rects1[0][0] < 48 and rects1[0][3] > y) or (rects2[0][0] < 48 and rects2[0][1] < y + 48):
            flag = True
    if flag or score == 10:
        flag = True
        rects1 = []
        rects2 = []
        screen.blit(over,(300,270))
        for i in range(score):
            screen.blit(birds[0],(i * 60 + 100,200))
        pg.display.update()
        continue
    pg.display.update()